package com.mukefile.dao.impl;

import com.mukefile.dao.LogDao;
import com.mukefile.pojo.Log;
import com.mukefile.pojo.MLog;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.query.NativeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.beans.Expression;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: kenewstar
 * @Description: 日志数据访问层
 * @Date:Created in 2020/9/11
 */
@Repository
public class LogDaoImpl implements LogDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Override
    public void insertLog(Log log){
        // 执行日志插入操作
        this.hibernateTemplate.save(log);
    }

    @Override
    public List<Log> selectLogByLimit(int limitStart,int limitEnd) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String sql = "SELECT * FROM log  ORDER BY logTime DESC LIMIT ?,?";
        List list = session.createSQLQuery(sql)
                .addEntity(Log.class)
                .setParameter(1,limitStart)
                .setParameter(2,limitEnd).list();
        return list;
    }

    @Override
    public Integer selectCount() {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String sql = "SELECT count(*) FROM log";
        String count = session.createSQLQuery(sql).list().get(0).toString();
        return Integer.parseInt(count);
    }

    @Override
    public List<Log> selectLogByCondition(Integer id, Date startTime, Date endTime, int limitStart, int limitEnd) {
        // 获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        // SQL
        StringBuilder sql = new StringBuilder("Select * from log where 1=1 ");
        if (Objects.nonNull(id)){
            sql.append("and uid=? ");
        }
        if (Objects.nonNull(startTime)){
            sql.append("and logTime>? ");
        }
        if (Objects.nonNull(endTime)){
            sql.append("and logTime<?");
        }
        sql.append(" ORDER BY logTime DESC LIMIT ?,?");
        // 执行查询
        NativeQuery query = session.createSQLQuery(sql.toString()).addEntity(Log.class);
        // 封装参数
        int index = 0;
        // 设置参数
        if (Objects.nonNull(id)){
            query.setParameter(++index,id);
        }
        if (Objects.nonNull(startTime)){
            query.setParameter(++index,startTime);
        }
        if (Objects.nonNull(endTime)){
            query.setParameter(++index,endTime);
        }
        List list = query.setParameter(++index, limitStart).setParameter(++index, limitEnd).list();
        return list;
    }

    @Override
    public Integer selectCountByCondition(Integer id, Date startTime, Date endTime) {
        // 获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        // SQL
        StringBuilder sql = new StringBuilder("select * from log where 1=1 ");
        if (Objects.nonNull(id)){
            sql.append("and uid=? ");
        }
        if (Objects.nonNull(startTime)){
            sql.append("and logTime>? ");
        }
        if (Objects.nonNull(endTime)){
            sql.append("and logTime<?");
        }
        NativeQuery query = session.createSQLQuery(sql.toString()).addEntity(Log.class);
        // 封装参数
        int index = 0;
        // 设置参数
        if (Objects.nonNull(id)){
            query.setParameter(++index,id);
        }
        if (Objects.nonNull(startTime)){
            query.setParameter(++index,startTime);
        }
        if (Objects.nonNull(endTime)){
            query.setParameter(++index,endTime);
        }
        List list = query.list();
        return list.size();
    }

    @Override
    public Integer selectMLogCountByCondition(Integer mid, Date startTime, Date endTime) {
        // 获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        // SQL
        StringBuilder sql = new StringBuilder("select * from mlog where 1=1 ");
        if (Objects.nonNull(mid)){
            sql.append("and mid=? ");
        }
        if (Objects.nonNull(startTime)){
            sql.append("and logTime>? ");
        }
        if (Objects.nonNull(endTime)){
            sql.append("and logTime<?");
        }
        NativeQuery query = session.createSQLQuery(sql.toString()).addEntity(MLog.class);
        // 封装参数
        int index = 0;
        // 设置参数
        if (Objects.nonNull(mid)){
            query.setParameter(++index,mid);
        }
        if (Objects.nonNull(startTime)){
            query.setParameter(++index,startTime);
        }
        if (Objects.nonNull(endTime)){
            query.setParameter(++index,endTime);
        }
        List list = query.list();
        return list.size();
    }

    @Override
    public List<MLog> selectMLogByCondition(Integer mid, Date startTime, Date endTime, int limitStart, int limitEnd) {
        // 获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        // SQL
        StringBuilder sql = new StringBuilder("Select * from mlog where 1=1 ");
        if (Objects.nonNull(mid)){
            sql.append("and mid=? ");
        }
        if (Objects.nonNull(startTime)){
            sql.append("and logTime>? ");
        }
        if (Objects.nonNull(endTime)){
            sql.append("and logTime<?");
        }
        sql.append(" ORDER BY logTime DESC LIMIT ?,?");
        // 执行查询
        NativeQuery query = session.createSQLQuery(sql.toString()).addEntity(MLog.class);
        // 封装参数
        int index = 0;
        // 设置参数
        if (Objects.nonNull(mid)){
            query.setParameter(++index,mid);
        }
        if (Objects.nonNull(startTime)){
            query.setParameter(++index,startTime);
        }
        if (Objects.nonNull(endTime)){
            query.setParameter(++index,endTime);
        }
        List list = query.setParameter(++index, limitStart).setParameter(++index, limitEnd).list();
        return list;
    }

}
